package com.paireach.szwolaiyun.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.util.LogUtils;
import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.mode.DispatchMode;
import com.paireach.szwolaiyun.mode.SignMode;
import com.paireach.szwolaiyun.sz_util.SharedPreferencesUtil;
import com.paireach.szwolaiyun.util.MessageInfoUtil;
import com.paireach.szwolaiyun.util.MyHttpUtil;
import com.paireach.szwolaiyun.util.StringUtil;

public class HomeFragment extends Fragment implements View.OnClickListener {
    private View view;
    private Handler handler;
    private Button bt_accept;
    private TextView main_userphone;//用户电话
    private TextView main_username;//用户名
    private TextView tv_exceptionphone;//异常电话

    private LinearLayout have_order_layout;//有运单时显示，无运单时隐藏
    private TextView tv_home_ordercode;//订单号
    private TextView tv_ordertime;//装运日期
    private TextView tv_originname;//发货工厂
    private TextView tv_origin_address;//发货工厂地址
    private TextView tv_endname;//收货工厂
    private TextView tv_end_address;//收货工厂地址
    private TextView tv_assign;//运单状态

    private TextView no_order;//暂无新的运单提示文字

    private DispatchMode mode;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, null);
        initinitControl();
        MyHttpUtil.getUserDispatch(getActivity(), httpHandle);//获取订单消息，并在这个方法里面发型消息，会到本页面调用handleMessage(...)
        Log.e("0011", "创建新的Fragment");
        return view;
    }

    private void initinitControl() {
        bt_accept = (Button) view.findViewById(R.id.bt_accept);
        main_userphone = (TextView) view.findViewById(R.id.main_userphone);
        main_username = (TextView) view.findViewById(R.id.main_username);
        tv_exceptionphone = (TextView) view.findViewById(R.id.tv_exceptionphone);
        tv_home_ordercode = (TextView) view.findViewById(R.id.tv_home_ordercode);
        tv_ordertime = (TextView) view.findViewById(R.id.tv_ordertime);
        have_order_layout = (LinearLayout) view.findViewById(R.id.have_order_layout);
        tv_originname = (TextView) view.findViewById(R.id.tv_originname);
        tv_origin_address = (TextView) view.findViewById(R.id.tv_origin_address);
        tv_endname = (TextView) view.findViewById(R.id.tv_endname);
        tv_end_address = (TextView) view.findViewById(R.id.tv_end_address);
        tv_assign = (TextView) view.findViewById(R.id.tv_assign);
        no_order = (TextView) view.findViewById(R.id.no_order);
        have_order_layout.setVisibility(View.INVISIBLE);

        bt_accept.setOnClickListener(this);

        main_userphone.setText(SharedPreferencesUtil.getValue(getActivity(), SharedPreferencesUtil.USER_PHONE));
        main_username.setText(SharedPreferencesUtil.getValue(getActivity(), SharedPreferencesUtil.USER_NAME));
        tv_exceptionphone.setText(SharedPreferencesUtil.getValue(getActivity(), SharedPreferencesUtil.HOTLINE));

    }

    public void initHandle(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_accept:
                Message message = new Message();
                if (mode.getStatusCode().equals(StringUtil.NOT_RECEIVE)) {
                    //显示运单接收，转到运单详情界面fragment
                    message.what = MessageInfoUtil.HOME_TO_WAYBILL;
                    message.obj = mode;
                    handler.sendMessage(message);
                } else if (mode.getStatusCode().equals(StringUtil.GO_TO_SHIPMENT)) {
                    message.what = MessageInfoUtil.Home_TO_SGIN;
                    SignMode signMode = new SignMode();
                    SignMode.changeDispatchModeToSignMode(signMode, mode);
                    message.obj = signMode;
                    handler.sendMessage(message);

                    //显示装货在途
                } else if (mode.getStatusCode().equals(StringUtil.LINE_UP)) {
                    //场外排队（装货工厂）
                    message.what = MessageInfoUtil.HOME_TO_QUEUE;

                    message.obj = mode;
                    handler.sendMessage(message);


                } else if (mode.getStatusCode().equals(StringUtil.READY_TO_LOAD)) {
                    //准备装货
                } else if (mode.getStatusCode().equals(StringUtil.IN_LOADING)) {
                    //装货中
                    message.what = MessageInfoUtil.HOME_TO_QUEUE;

                    message.obj = mode;
                    handler.sendMessage(message);


                } else if (mode.getStatusCode().equals(StringUtil.LEAVE_THE_FACTORY)) {
                    //离场中(装货工厂)
                } else if (mode.getStatusCode().equals(StringUtil.IN_DELIVERY)) {
                    //送货在途
                    message.what = MessageInfoUtil.Home_TO_SGIN;

                    SignMode signMode = new SignMode();
//                    signMode.setDate(mode.getDate().replace("-","/")+"   "+mode.getPlanDeliverStartTime()+"-"+mode.getPlanDeliverEndTime());
//                    signMode.setStatusCode(mode.getStatusCode());
//                    signMode.setStatusName(mode.getStatusName());
//                    signMode.setAddress(mode.getDeliveryPlantAddress());
//                    signMode.setLat(mode.getSourceLat());
//                    signMode.setLon(mode.getSourceLng());
//                    signMode.setWarehouseType(mode.getWarehouseType());
//                    signMode.setScSupportBking(mode.getScSupportBking());
//                    signMode.setCode(mode.getCode());

                    SignMode.changeDispatchModeToSignMode(signMode, mode);
                    message.obj = signMode;
                    handler.sendMessage(message);

                } else if (mode.getStatusCode().equals(StringUtil.LINE_UP_DESTINATION)) {
                    //场外排队(收货工厂)
                } else if (mode.getStatusCode().equals(StringUtil.READY_TO_RECEIPT)) {
                    //准备收货
                } else if (mode.getStatusCode().equals(StringUtil.RECEIVING)) {
                    message.what = MessageInfoUtil.HOME_TO_QUEUE;
                    message.obj = mode;
                    handler.sendMessage(message);
                    //收货中  需要一个HOME_TO_SIGN属性；
                } else if (mode.getStatusCode().equals(StringUtil.LEAVE_FACTORY)) {
                    //离场中（收货工厂）
                } else if (mode.getStatusCode().equals(StringUtil.ALREADY_SIGN)) {
                    //已签收
                } else if (mode.getStatusCode().equals(StringUtil.HAS_BEEN_COMPLETED)) {
                    //已完成
                }
                break;
        }
    }

    private Handler httpHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MessageInfoUtil.GET_DISPATCH_SUCCEED:
                    mode = new DispatchMode();
                    mode.init(msg.obj.toString());
                    if (mode.getIsHaveDispatch()) {
                        have_order_layout.setVisibility(View.VISIBLE);
                        tv_home_ordercode.setText(mode.getCode());
                        tv_ordertime.setText(mode.getDate());
                        tv_originname.setText(mode.getDeliveryPlant());
                        tv_origin_address.setText(mode.getDeliveryPlantAddress());
                        tv_endname.setText(mode.getReceivingPlant());
                        tv_end_address.setText(mode.getReceivingPlantAddress());
                        tv_assign.setText(mode.getStatusName());
                        no_order.setVisibility(View.INVISIBLE);
                        bt_accept.setEnabled(true);
                        if (mode.getStatusCode().equals("212")) {//StringUtil:212运单未接收
                            bt_accept.setText("接收运单");
                        } else {
                            bt_accept.setText("运单详情");
                        }
                    } else {
                        no_order.setVisibility(View.VISIBLE);
                        bt_accept.setText("暂无运单");
                        bt_accept.setEnabled(false);
                    }

                    break;
                case MessageInfoUtil.GET_DISPATCH_FAIL:
                    no_order.setVisibility(View.VISIBLE);
                    bt_accept.setText("暂无运单");
                    bt_accept.setEnabled(false);
                    break;
            }
        }
    };
}
